/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.DAO;

import com.webaccess.retailws.Bean.ImageBean;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.webaccess.retailws.db.DBConnection;
import java.util.*;

/**
 *
 * @author Owner
 */
public class AdminDAO {

    DBConnection dbc = null;
    Connection conn = null;
    ResultSet rs = null;
    String result = null;
    StringBuffer myQuery = null;
    ImageBean imagBean = null;

    public AdminDAO() {
        try {
            dbc = new DBConnection();
            //  conn = (Connection) dbc.getConnection();

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public ImageBean[] getAllHomePageImage() {
        ImageBean[] imageList = null;
        imagBean = new ImageBean();
        try {
            if (conn == null || conn.isClosed()) {
                conn = (Connection) dbc.getConnection();
            }
            myQuery = new StringBuffer();
            myQuery.append("CALL sp_selectHomePageImage('0')");
            rs = dbc.executeProcedure(conn, myQuery.toString());
            if (rs != null) {
                String rsValue = String.valueOf(rs);
                if (!rsValue.equalsIgnoreCase("Result set representing update count of 0")) {
                    imageList = setAllImage(rs);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbc.freeResultSet(rs);
                dbc.freeConnection(conn);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return imageList;
        }
    }

    private ImageBean[] setAllImage(ResultSet rs) {
        List alImage = new ArrayList();
        try {
            alImage.clear();
            while (rs.next()) {
                imagBean = new ImageBean();
                imagBean.setImageId(rs.getLong("image_id"));
                imagBean.setHomePageImageName(rs.getString("imagefilename"));
                imagBean.setImageName(rs.getString("imagename"));
                imagBean.setImageDetail(rs.getString("description"));
                alImage.add(imagBean);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            return (ImageBean[]) alImage.toArray(new ImageBean[0]);
        }
    }

    public ImageBean getImage(Long imageId) {
        imagBean = new ImageBean();
        try {
            if (conn == null || conn.isClosed()) {
                conn = (Connection) dbc.getConnection();
            }
            myQuery = new StringBuffer();
            myQuery.append("CALL sp_selectHomePageImage('");
            myQuery.append(imageId);
            myQuery.append("')");
            rs = dbc.executeProcedure(conn, myQuery.toString());
            if (rs != null) {
                String rsValue = String.valueOf(rs);
                if (!rsValue.equalsIgnoreCase("Result set representing update count of 0")) {
                    imagBean = setImage(rs);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbc.freeResultSet(rs);
                dbc.freeConnection(conn);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return imagBean;
        }
    }

    private ImageBean setImage(ResultSet rs) {
        try {
            while (rs.next()) {
                imagBean = new ImageBean();
                imagBean.setImageId(rs.getLong("image_id"));
                imagBean.setHomePageImageName(rs.getString("imagefilename"));
                imagBean.setImageName(rs.getString("imagename"));
                imagBean.setImageDetail(rs.getString("description"));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            return imagBean;
        }
    }

    public boolean deleteImage(Long imageId) {
        boolean isImageDeleted = false;
        try {
            if (conn == null || conn.isClosed()) {
                conn = (Connection) dbc.getConnection();
            }
            myQuery = new StringBuffer();
            myQuery.append("CALL sp_deleteHomePageImage('");
            myQuery.append(imageId);
            myQuery.append("')");
            rs = dbc.executeProcedure(conn, myQuery.toString());
            if (rs != null) {
                isImageDeleted = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbc.freeResultSet(rs);
                dbc.freeConnection(conn);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return isImageDeleted;
        }
    }

    public boolean saveImage(ImageBean imagBean) {
        boolean isImageSaved = false;
        Long imageId = 0L;
        try {
            if (conn == null || conn.isClosed()) {
                conn = (Connection) dbc.getConnection();
            }
            myQuery = new StringBuffer();
            if (imagBean.getImageId() == 0) {
                myQuery.append("CALL sp_savehomepageimage(0,'");
            } else {
                myQuery.append("CALL sp_savehomepageimage('");
                myQuery.append(imagBean.getImageId());
                myQuery.append("','");
            }
            myQuery.append(imagBean.getImageName());
            myQuery.append("','");
            myQuery.append(imagBean.getImageDetail());
            myQuery.append("','");
            myQuery.append(imagBean.getHomePageImageName());
            myQuery.append("')");
            rs = dbc.executeProcedure(conn, myQuery.toString());
            if (imagBean.getImageId() == null) {
                if (rs != null) {
                    String rsValue = String.valueOf(rs);
                    if (!rsValue.equalsIgnoreCase("Result set representing update count of 0")) {
                        while (rs.next()) {
                            imageId = rs.getLong("sp_ImageId");
                        }
                    }
                }
                if (imageId > 0) {
                    isImageSaved = true;
                }
            } else {
                isImageSaved = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbc.freeResultSet(rs);
                dbc.freeConnection(conn);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return isImageSaved;
        }
    }
}
